package cn.time1024.time_app.ui.notifications

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.timeassistant.network.ApiService
import cn.time1024.time_app.R

class NotificationsAdapter(
    private val items: MutableList<ApiService.NotificationItem> = mutableListOf(),
    private val onClick: (ApiService.NotificationItem) -> Unit
) : RecyclerView.Adapter<NotificationsAdapter.VH>() {

    class VH(itemView: android.view.View) : RecyclerView.ViewHolder(itemView) {
        val tvTitle: TextView = itemView.findViewById(R.id.tvTitle)
        val tvMeta: TextView = itemView.findViewById(R.id.tvMeta)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_notification, parent, false)
        return VH(view)
    }

    override fun getItemCount(): Int = items.size

    override fun onBindViewHolder(holder: VH, position: Int) {
        val item = items[position]
        holder.tvTitle.text = item.title ?: "(无标题)"
        val type = item.type ?: "未知"
        val time = item.createTime ?: ""
        holder.tvMeta.text = "$type · $time"
        holder.itemView.setOnClickListener { onClick(item) }
    }

    fun replaceAll(list: List<ApiService.NotificationItem>) {
        items.clear(); items.addAll(list); notifyDataSetChanged()
    }

    fun appendAll(list: List<ApiService.NotificationItem>) {
        val start = items.size; items.addAll(list); notifyItemRangeInserted(start, list.size)
    }
}